define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'purchase/index' + location.search,
                    add_url: 'purchase/add',
                    adds_url: 'purchase/adds',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'purchase/multi',
                    import_url: 'purchase/import',
                    table: 'purchase',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'purchase_no', title: __('进货单号'), operate: 'LIKE'},
                        {field: 'create_admin_name', title: __('创建者'), operate: false},
                        {field: 'status', title: __('状态'), searchList: {1:__('待审核'),2:__('入库'),3:__('作废')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name:'detail',//名称
                                    text:'处理单据',
                                    title:'处理单据',
                                    classname: 'btn btn-info btn-view',
                                    // icon: 'fa fa-check',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'purchasedetail/index?purchase_id={id}&p_status={status}#',//接口地址 控制器名/方法名
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                },
                                {
                                    name:'show',//名称
                                    text:'查看单据',
                                    title:'查看单据',
                                    classname: 'btn btn-info bg-teal',
                                    // icon: 'fa fa-check',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'purchasedetail/index?purchase_id={id}&p_status={status}#',//接口地址 控制器名/方法名
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.status != 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
